package com.wxmit.module.system.web;

import cn.hutool.core.lang.tree.Tree;
import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.web.operatelog.Log;
import com.wxmit.framework.web.operatelog.enums.BusinessType;
import com.wxmit.base.validate.AddGroup;
import com.wxmit.base.validate.UpdateGroup;
import com.wxmit.module.system.domain.SysMenu;
import com.wxmit.module.system.domain.dto.BatchAddBtnDto;
import com.wxmit.module.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  菜单模块
 * @author huangxiong
 * @date 2022/7/19 17:27
 * @version V1.0
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @GetMapping("/tree")
    public List<Tree<Long>> getMenuTree(){
       return sysMenuService.listMenuTree();
    }

    @GetMapping("/routerTree")
    public List<Tree<Long>> getMenuRouterTree(){
        return sysMenuService.listRouterTree();
    }

    @PostMapping
    @Log(module="菜单管理",name="新增菜单",type = BusinessType.INSERT)
    public ApiResult add(@RequestBody @Validated(AddGroup.class) SysMenu sysMenu){
        return ApiResult.successWithData(sysMenuService.save(sysMenu));
    }

    @PutMapping
    @Log(module="菜单管理",name="修改菜单",type = BusinessType.UPDATE)
    public ApiResult update(@RequestBody @Validated(UpdateGroup.class) SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return ApiResult.success();
    }

    @DeleteMapping("/{id}")
    @Log(module="菜单管理",name="删除菜单",type = BusinessType.DELETE)
    public ApiResult add(@PathVariable("id") Long id){
        sysMenuService.removeById(id);
        return ApiResult.success();
    }

    @PostMapping("/batchAddBtn/{pId}")
    @Log(module="菜单管理",name="批量新增按钮",type = BusinessType.INSERT)
    public ApiResult batchAddBtn(@RequestBody @Validated List<BatchAddBtnDto> list,@PathVariable("pId") Long pId){

        return ApiResult.of(sysMenuService.batchAddBtn(pId,list));
    }
}
